package org.qy.star_common.message;


import java.util.Objects;
import java.util.UUID;

/**
 * @author yinger
 * @description TODO
 * @date 2024/3/26 07:41
 **/
public class Message {
    /*属性*/
    /**************************************************************************************************************/
    private MessageProperties messageProperties = new MessageProperties(); // 消息基本属性
    private byte[] body;                                                    // 消息正文
    private long offsetBegin = 0;                                           // 文件消息起始偏移值
    private long offsetEnd = 0;                                             // 文件消息结束偏移值
    private byte valid = (byte) 0x1;                                        // 文件消息有效标志位
    private String message;                                                 // 处理前端传来等的消息
    /*构造*/

    /**************************************************************************************************************/
    public Message(MessageProperties messageProperties, byte[] byteArray, byte valid) {
        this.messageProperties = messageProperties;
        this.body = byteArray;
        this.valid = valid;
    }

    public Message(MessageProperties messageProperties, byte[] body) {
        this.messageProperties = messageProperties;
        this.body = body;

        // check 字段
        if (messageProperties == null) {
            this.messageProperties = new MessageProperties();
        }

        if (this.messageProperties.getMessageId() == null) {
            this.messageProperties.setMessageId("");
        }

        if (this.messageProperties.getRoutingKey() == null) {
            this.messageProperties.setRoutingKey("");
        }

        if (this.body == null) {
            this.body = new byte[0];
        }


    }



    public void setValid(int valid) {
        this.valid = (byte) valid;
    }

    public int getDeliveryMode() {
        return messageProperties.getDeliveryMode();
    }

    public String getMessageId() {
        return messageProperties.getMessageId();
    }

    public String getRoutingKey() {
        return messageProperties.getRoutingKey();
    }

    public void setMessageId(String messageId) {
        messageProperties.setMessageId(messageId);
    }

    public void setRoutingKey(String routingKey) {
        messageProperties.setRoutingKey(routingKey);
    }

    public void setDeliveryMode(int deliveryMode) {
        messageProperties.setDeliveryMode(deliveryMode);
    }

    public MessageProperties getMessageProperties() {
        return messageProperties;
    }

    public void setMessageProperties(MessageProperties messageProperties) {
        this.messageProperties = messageProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBegin() {
        return offsetBegin;
    }

    public void setOffsetBegin(long offsetBegin) {
        this.offsetBegin = offsetBegin;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getValid() {
        return valid;
    }

    public void setValid(byte valid) {
        this.valid = valid;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static Message createMessageWithId(MessageProperties basicProperties, byte[] body, String routingKey) {
        Message message = new Message();
        if (basicProperties != null) {
            message.messageProperties = basicProperties;
        }
        message.messageProperties.setMessageId("M-" + UUID.randomUUID());
        message.messageProperties.setRoutingKey(routingKey);
        message.body = body;
        return message;
    }

    public Message() {
        this(null,null);
    }
}
